Ext.BLANK_IMAGE_URL = "../script/ext-2.0.2/resources/images/default/s.gif";
var paramFristStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({url: 'children/class!list.action?dateTime=' + new Date().getTime()}),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        fields: ["id", "name"]
    }),
    remoteSort: true
});
paramFristStore.load();

var paramFrist = new Ext.form.ComboBox({
    id: 'paramFrist',
    name: 'paramFrist',
    fieldLabel: '班级',
    hiddenName: 'frist',
    store: paramFristStore,
    valueField: 'id',
    displayField: 'name',
    typeAhead: true,
    mode: 'local',
    triggerAction: 'all',
    resizable: true,
    emptyText: '请选择班级...',
    allowBlank: true,
    width: 150,
    forceSelection: false
});

var studentNameTemp = new Ext.form.TextField({
    id: 'studentNameTemp',
    name: 'studentNameTemp',
    width: 100
});

var storestudentManage = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'children/reduction!list.action?dateTime=' + new Date().getTime()
    }),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["stuName", "className", "money", "stuId", "id"]
    }),
    sortInfo: {field: 'harmDate', direction: 'DESC'},// 排序信息
    remoteSort: true
});

storestudentManage.load();

var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});

var colstudentManage = new Ext.grid.ColumnModel([sm,
    {header: "学生姓名", dataIndex: "stuName", width: 10, sortable: true},
    {header: "班级", dataIndex: "className", width: 10, sortable: true},
    {header: "欠费金额", dataIndex: "money", width: 10, sortable: true},
    {header: "stuId", dataIndex: "stuId", width: 10, sortable: true, hidden: true},
    {header: "ID", dataIndex: "id", width: 10, sortable: true, hidden: true}
]);

var oneTbar = new Ext.Toolbar({
    items: ['-', {text: '&nbsp;&nbsp;查&nbsp;&nbsp;询&nbsp;&nbsp;', iconCls: 'yes', handler: getList},
        '-', {text: '&nbsp;&nbsp;减&nbsp;&nbsp;免&nbsp;&nbsp;', iconCls: 'remove', handler: delinfo}
    ]
});

var studentManage = new Ext.grid.GridPanel({
    id: 'studentManage',
    title: '[学生管理]信息维护',
    height: 500,
    stripeRows: true,
    bodyStyle: 'width:100%',
    disableSelection: true,
    cm: colstudentManage,
    viewConfig: {
        forceFit: true
    },
    store: storestudentManage,
    iconCls: 'details',
    animCollapse: true,
    trackMouseOver: false,
    loadMask: {msg: '正在加载[欠费学生记录]信息，请稍侯……'},
    selModel: sm,
    tbar: [
        '-', new Ext.form.Label({text: '班级:'}), '-', paramFrist,
        '-', new Ext.form.Label({text: '学生姓名:'}), '-', studentNameTemp,
    ],
    listeners: {
        'render': function () {
            oneTbar.render(this.tbar); //add one tbar
        }
    },
    bbar: new Ext.PagingToolbar({
        pageSize: 20,
        store: storestudentManage,
        displayInfo: true,
        emptyMsg: '没有记录'
    })
});
var reductionInfoProjectStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({url: 'children/reduction!getProjectListByStuId.action?dateTime=' + new Date().getTime()}),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["id", "name"]
    }),
    remoteSort: true
});
var reductioniInfoProjectComboBox = new Ext.form.ComboBox({
    id: 'projectId',
    name: 'projectId',
    fieldLabel: '项目名称',
    hiddenName: 'reductioniProjectId',
    store: reductionInfoProjectStore,
    valueField: 'id',
    displayField: 'name',
    typeAhead: true,
    mode: 'local',
    triggerAction: 'all',
    resizable: true,
    emptyText: '请选择项目...',
    allowBlank: false,
    width: 150,
    forceSelection: true
});
Ext.apply(Ext.form.VTypes, {
    money: function (val, field) {//val指这里的文本框值，field指这个文本框组件，大家要明白这个意思
        var regu = /^[-]\d*\.?\d{0,2}$/
        // console.log(regu.test(val))
        if (val != "") {
            if (!regu.test(val)) {
                return false
            } else {
                return true
            }
        }
        return false;
    }
});

var reductioniInfoFp = new Ext.FormPanel({
    id: 'ext_reductioniInfoFp',
    labelWidth: 100,
    waitMsg: '正在提交，请稍后...',
    frame: true,
    bodyStyle: 'padding:5px 5px 0',
    width: 410,
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["name"]
    }),
    items: [{
        layout: 'form',
        defaultType: 'textfield',
        items: [{
            fieldLabel: '学生姓名',
            name: 'name',
            id: 'name',
            maxLength: 10,
            anchor: '95%',
            blankText: "学生姓名不可以为空!",
            allowBlank: false
        }, reductioniInfoProjectComboBox, {
            fieldLabel: '减免金额',
            name: 'money',
            id: 'money',
            vtype: 'money',
            vtypeText: '减免金额只能为负值！',
            maxLength: 10,
            anchor: '95%',
            blankText: "减免金额不可以为空!",
            allowBlank: false
        },
            new Ext.form.Hidden({name: 'stuId'}), new Ext.form.Hidden({name: 'id'})
        ]
    }]
});

var reductionInfoWin = new Ext.Window({
    id: "ext_reductioniInfoWin",
    title: '[减免信息]',
    width: 410,
    closeAction: 'hide',
    modal: true,
    buttons: [{
        text: '提交',
        handler: function () {
            //alert( reductioniInfoFp.getForm().isValid() )
            if (reductioniInfoFp.getForm().isValid()) {
                reductioniInfoFp.form.submit({
                    url: 'children/reduction!saveReduction.action',
                    waitTitle: "新增减免",
                    waitMsg: '正在提交数据,请稍后... ...',
                    success: function () {
                        Ext.getCmp('studentManage').getStore().reload(); //刷新Grid
                        reductioniInfoFp.form.reset();
                        Ext.MessageBox.alert('提示信息', '维护减免信息成功！');
                        reductionInfoWin.hide();
                    },
                    failure: function (form, action) {
                        Ext.MessageBox.alert('提示信息', '维护减免信息失败,请联系系统管理员!');
                    }
                });
            }
        }
    }, {
        text: '取消', handler: function () {
            reductionInfoWin.hide();
        }
    }],
    items: [reductioniInfoFp]
});

function delinfo() {
    var records = studentManage.getSelectionModel().getSelected();
    var stuId = records.data["stuId"];
    var stuName = records.data["stuName"]
    var id = records.data["id"]
    var params = {};
    params.stuId = stuId;
    reductionInfoProjectStore.baseParams = params;
    reductionInfoProjectStore.load({
        callback: function (r, options, success) {
            if (success == false) {
                Ext.Msg.alert("错误", storeCost.reader.jsonData.msg);
            }
        }
    });
    reductionInfoWin.show()
    reductioniInfoFp.getForm().findField("name").setValue(stuName);
    reductioniInfoFp.getForm().findField("stuId").setValue(stuId);
    reductioniInfoFp.getForm().findField("id").setValue(id)
}


var paramClass = new Ext.form.ComboBox({
    id: 'paramClass',
    name: 'paramClass',
    fieldLabel: '班级',
    hiddenName: 'classId',
    store: paramFristStore,
    valueField: 'id',
    displayField: 'name',
    typeAhead: true,
    mode: 'local',
    triggerAction: 'all',
    resizable: true,
    emptyText: '请选择班级...',
    allowBlank: false,
    width: 150,
    forceSelection: true
});
studentManage.on('contextmenu', function (event) {//声明菜单类型
    if (studentManage.getSelectionModel().hasSelection()) {
        event.preventDefault();//关闭默认的菜单，以避免弹出两个菜单
        bdClick.showAt(event.getXY());//取得鼠标点击坐标，展示菜单
    }
}, studentManage);

var bdClick = new Ext.menu.Menu({
    id: 'bdClickCont',
    items: [{
        iconCls: 'yes',
        text: '欠费明细',
        handler: costList
    }, {
        iconCls: 'yes',
        text: '减免明细',
        handler: reductionList
    }]
});

var storePaymentPManage = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'children/reduction!getProjectListByStuId.action'
    }),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["name", "preMoney", "id"]
    }),
});
var colCost = new Ext.grid.ColumnModel([{
    header: "缴费项目名称",
    dataIndex: "name",
    width: 100,
    sortable: true
}, {header: "欠费金额", dataIndex: "preMoney", width: 100, sortable: true}]);
var storeCost = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({url: 'children/reduction!getProjectListByStuId.action?dateTime=' + new Date().getTime()}),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["name", "preMoney"]
    }),
    remoteSort: true
});
var costManage = new Ext.grid.GridPanel({
    id: 'costManage',
    height: 200,
    stripeRows: true,
    bodyStyle: 'width:100%',
    disableSelection: true,
    cm: colCost,
    viewConfig: {forceFit: false},
    store: storeCost,
    iconCls: 'details',
    animCollapse: true,
    trackMouseOver: false,
    loadMask: {msg: '正在加载[欠费项目]信息，请稍侯……'}
});
var fpCost = new Ext.FormPanel({
    id: 'ext_formCost',
    labelWidth: 100,
    waitMsg: '正在提交，请稍后...',
    frame: true,
    fileUpload: true,
    width: 300,
    items: [costManage]
});
var costWin = new Ext.Window({
    id: "ext_winCost",
    title: '[欠费项目]',
    width: 300,
    closeAction: 'hide',
    modal: true,
    items: [fpCost]
});

function costList() {
    var records = studentManage.getSelectionModel().getSelected();
    var stuId = records.data["stuId"];

    var params = {};
    params.stuId = stuId;
    storeCost.baseParams = params;
    storeCost.load({
        callback: function (r, options, success) {
            if (success == false) {
                Ext.Msg.alert("错误", storeCost.reader.jsonData.msg);
            }
        }
    });
    costWin.show();
}


//----------------------------------------


var reductionCost = new Ext.grid.ColumnModel([{
    header: "减免项目名称",
    dataIndex: "name",
    width: 100,
    sortable: true
}, {header: "减免金额", dataIndex: "preMoney", width: 100, sortable: true}]);
var reductionStoreCost = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({url: 'children/reduction!getProjectListByStuId.action?dateTime=' + new Date().getTime()}),
    reader: new Ext.data.JsonReader({
        root: 'ZANY',
        totalProperty: "results",
        fields: ["name", "preMoney"]
    }),
    remoteSort: true
});
var reductionManage = new Ext.grid.GridPanel({
    id: 'reductionManage',
    height: 200,
    stripeRows: true,
    bodyStyle: 'width:100%',
    disableSelection: true,
    cm: reductionCost,
    viewConfig: {forceFit: false},
    store: reductionStoreCost,
    iconCls: 'details',
    animCollapse: true,
    trackMouseOver: false,
    loadMask: {msg: '正在加载[减免项目]信息，请稍侯……'}
});
var reductionFpCost = new Ext.FormPanel({
    id: 'ext_formreduction',
    labelWidth: 100,
    waitMsg: '正在提交，请稍后...',
    frame: true,
    fileUpload: true,
    width: 300,
    items: [reductionManage]
});
var reductionWin = new Ext.Window({
    id: "ext_winreduction",
    title: '[减免项目]',
    width: 300,
    closeAction: 'hide',
    modal: true,
    items: [reductionFpCost]
});

function reductionList() {
    var records = studentManage.getSelectionModel().getSelected();
    var stuId = records.data["stuId"];
    var params = {};
    params.stuId = stuId;
    params.chargesType = 2
    reductionStoreCost.baseParams = params;
    reductionStoreCost.load({
        callback: function (r, options, success) {
            if (success == false) {
                Ext.Msg.alert("错误", storeCost.reader.jsonData.msg);
            }
        }
    });
    reductionWin.show();
}

function insertinfo() {
    winKey.show();
}


studentManage.addListener('rowdblclick', function (e) {
        if (Ext.getCmp("studentManage").getSelectionModel().hasSelection()) {
            costList();
        }
    }, studentManage
);

function getDetail() {
    // var win = new Ext.Window({
    //     layout: 'fit',
    //     width: 500,
    //     height: 200,
    //     closeAction: 'hide',
    //     draggable: true,
    //     items: [{title:'窗口示例',html:'<div>  </div>'}]
    // });
    // win.show();
    //
    //
    var params = {stuId: 6}
    storePaymentPManage.baseParams = params
    storePaymentPManage.load()
    winKey.show()
    // // Ext.getCmp('projectManage').getStore().reload();
    // // storePaymentPManage.load({
    // //     params: {stuId: 6}
    // // });
    // var storePaymentPManage1 = new Ext.data.Store({
    //     proxy: new Ext.data.HttpProxy({
    //         url: 'children/reduction!getProjectListByStuId.action'
    //     }),
    //     reader: new Ext.data.JsonReader({
    //         root: 'ZANY',
    //         totalProperty: "results",
    //         fields: ["name", "preMoney", "id"]
    //     }),
    // });

    // storePaymentPManage1.load({
    //     callback : function(r, options, success) {
    //         grid_tab.reconfigure(storePaymentPManage1,new Ext.grid.ColumnModel([
    //             {header: "缴费2项目", dataIndex: "name", width: 10},
    //             {header: "金额", dataIndex: "preMoney", width: 10}
    //         ]));
    //     }
    // });

}

function resetQuery() {
    Ext.getCmp("studentNameTemp").setValue('');
    orgTree.setValue('');
}

function getList() {
    if (paramFrist.getRawValue() == '') {
        paramFrist.clearValue()
    }
    var params = {};
    var classId = paramFrist.getValue();
    params.classId = classId
    params.name = Ext.get('studentNameTemp').getValue();
    storestudentManage.baseParams = params;
    storestudentManage.load({
        callback: function (r, options, success) {
            if (success == false) {
                Ext.Msg.alert("错误", storestudentManage.reader.jsonData.msg);
            }
        }
    });
}

Ext.onReady(function () {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var port = new Ext.Viewport({
        layout: 'fit',
        frame: true,
        items: [this.studentManage]
    });
});